package problema056;

import java.math.BigInteger;

public class PowerfulDigitSum {
	
	static int res = 0;
	static BigInteger limit = new BigInteger("100");
	static BigInteger uno = new BigInteger("1");
	static BigInteger diez = new BigInteger("10");
	
	public static void main(String[] args){
		System.out.println(dameSumaMasGrande());
	}
	
	public static int dameSumaMasGrande(){
		BigInteger a = BigInteger.ZERO, b = BigInteger.ZERO, n = BigInteger.ZERO;
		int cant = 0;
		for(; !a.equals(limit); a = a.add(uno)){
			for(; !b.equals(limit); b = b.add(uno)){
				n = a.pow(b.intValue());
				cant = sumaDigitos(n);
				if(cant > res) res = cant;
			}
			b = BigInteger.ZERO;
		}
		return res;
	}
	
	public static int sumaDigitos(BigInteger n){
		int cant = 0;
		BigInteger act = BigInteger.ZERO;
		while (!n.equals(BigInteger.ZERO)) {
		      act = n.mod(diez);
		      cant += act.intValue();
		      n = n.divide(diez);
		 }
		return cant;
	}

}
